<?
require_once('../includes/appInit.php');
include('../includes/ajax/ajaxResources.php');


$catID = $_GET['cID'];
//$fCategories = getCategories('select','F',$catID);

// Look for sub-categories coming in
//$fSubCats = getSubCategories('select','F',$catID);
if ($fSubCats == '0') {
	$subCatContainerDisplay = 'none';
} else {
	$subCatContainerDisplay = '';
}

if ($_POST[upload] == 1) {
	$fileCat    = $_POST[fileCat];
	$fileSubCat = $_POST[subCatSelect]; 
	$fileTitle  = fieldToDB($_POST[fileTitle]);
	$fileDesc   = fieldToDB($_POST[fileDesc]);
		
	$error = 0;
	$upload = 1;
		
	$someFile     = $_FILES['fileUpload']['tmp_name'];
	$someFileName = $_FILES['fileUpload']['name'];
	$someFileType = $_FILES['fileUpload']['type'];
	$someFileSize = $_FILES['fileUpload']['size'];
		
	// Clean Filename
	$filename = cleanString($someFileName);		
	$fileDir  = $rootDir.'docs/Shared Files/';
	$filePath = $rootDir.'docs/Shared Files/'.$filename;
	
	if ($error != 1) {
		if(move_uploaded_file($someFile, $filePath)){
			// Add file to database
			if ($_SESSION['userLevel'] == 1) {
				$approved = 1;
			} else {
				$approved = 0;
			}
			
			$sql = "INSERT into casa_shared_files (
					CatID,
					SubCatID,
					FileTitle,
					FileDesc,
					FileName,
					FileDate,
					FileSize,
					FileType,
					Approved
					) values (
					'$fileCat',
					'$fileSubCat',
					'$fileTitle',
					'$fileDesc',
					'$filename',
					'".date('Y-m-d')."',
					'$someFileSize',
					'$someFileType',					
					'$approved'
					)";
			$result = mysql_db_query($dbname,$sql);			
		} else {
			$error = 1;
		}
	}	
} else {
	$error = 0;
	$upload = 0;	
}

if (empty($fileCat)) {
	$fileCat = 0;
}
if (empty($fileSubCat)) {
	$fileSubCat = 0;
}
?>

<html><head>
	<script src="../js/protaculous/prototype1.6.js" language="JavaScript" type="text/javascript"></script>
	<script src="../js/protaculous/scriptaculous.js?load=effects,dragdrop,combo,controls" language="JavaScript" type="text/javascript"></script>
	
	<!-- WYSIWYG Editor stuff -->
	<script src="../js/tinyeditor/tiny_mce.js" language="javascript" type="text/javascript"></script>					
	<script src="../js/tinyeditor/config/mceConfig.js" language="javascript" type="text/javascript"></script>
	<!-- End WYSIWYG Editor stuff -->	
	<SCRIPT src="../js/jsCommon.js" language="JavaScript" type="text/javascript"></SCRIPT>
	
	<script language="JavaScript" type="text/javascript">
		var par = window.parent.document;
		
		function uploadComplete(upload,error) {
			if (upload == 1) {
				var progressDiv = par.getElementById('uploadStatus<?=$catID; ?>');
				if (error == 1) {
					progressDiv.innerHTML   = '<img src="../images/iconError.gif"> <?=$errorMessage; ?>';
				} else {
					// Refresh file list
					window.parent.filesViewObj.refreshFileList(<?=$fileCat; ?>,<?=$fileSubCat; ?>);
					progressDiv.innerHTML   = '<img src="../images/iconStatusComplete.gif"> <b>Your Upload Was Successful!</b>';
				}
			}
		}
		function uploadFile(){	
			var iframe = par.getElementById('fileFrame');
			var form   = document.getElementById('iform');
					
			var progressDiv = par.getElementById('uploadStatus<?=$catID; ?>');
			progressDiv.innerHTML = '<img src="../images/ajaxAnimBar.gif"> Your file is uploading...';
				
			// send
			form.submit();	
		}
		
		function getSubCategories(catID) {
			var params = 'handler=getSubCategories&display=json&siteArea=F&catID='+catID;
			
			var ajaxResults = new Ajax.Request('functions.php', {
				method: 'POST',
				parameters: params,	
				onSuccess: function(transport) {
					returnGetSubCategories(transport.responseText);
				},
				onFailure: function() {
					alert('Oops!');
				}		
			});
		}
	
		function returnGetSubCategories(ajaxResult) {
			if (ajaxResult != '0') {
				var jsonObject = eval('(' + ajaxResult + ')');
				var totalCategories = (jsonObject.categories.length)-1;
				for(i=0;i<=totalCategories;i++) {
					var catID   = jsonObject.categories[i].CID;
					var catName = jsonObject.categories[i].CatName;
					$('subCatSelect').options[i] = new Option(catName,catID);
				}
				$('subCatContainer').style.display = '';
			} else {
				$('subCatContainer').style.display = 'none';
			}
		}
		function jScriptInit() {
			// Turn on iFrame if necessary
			var par = window.parent.document;
			var iframe = par.getElementById('fileFrame<?=$catID; ?>');
			if (iframe.style.display == 'none') {
				iframe.style.display = 'block';
			}
			
			/*
			 * Init TinyMCE textarea
			 */
		}
	</script>
<link rel="stylesheet" type="text/css" href="../css/browserReset.css?r=<? echo random_string(5,'numeric'); ?>">
<link rel="STYLESHEET" media="screen" href="../css/stylesMozilla.css?r=<? echo random_string(5,'numeric'); ?>" type="text/css" />	
</head>
<body onload="jScriptInit(); uploadComplete(<?=$upload; ?>,<?=$error; ?>);">
<div>
	<form name="iform" action="<?=$_SERVER['PHP_SELF']; ?>?cID=<?=$catID; ?>" id="iform" method="post" enctype="multipart/form-data">
	<input type="hidden" name="upload" value="1">
		
	<input type="hidden" id="formAction" />
			<input type="hidden" id="fID" />				
			<p>
				<input type="checkbox" id="ptiSharedResource" /> <b>This is a PTI Shared Resource</b>
			</p>
					
			<p><span class="vertLabelSmall">Category &nbsp;&nbsp;<a href="#" id="newCatLink" onClick="window.parent.resourcesSupportObj.newCategoryForm(); return false;">New Category</a></span>
				<select id="fileCat" name="fileCat" size="1" style="width: 400px;" onChange="getSubCategories(this.options[this.selectedIndex].value); return false;">
				<?=$fCategories; ?>
				</select>
				<input type="text" style="width: 400px; display: none;" name="newCatField" id="newCatField" />
			</p>
			
			<p><span class="vertLabelSmall">Title</span>
			<input type="text" style="width: 400px;" id="fileTitle" name="fileTitle" /></p>
			
			<p><span class="vertLabelSmall">Short description</span>
			<textarea name="fileDesc" class="editorSimple" id="fileDesc" style="width: 500px; height: 150px;"></textarea>
			</p>			

			<p><span class="vertLabelSmall">Select File</span>
				<input type="file" id="fileUpload" name="fileUpload" size="50" />
			</p>		
			
			<div style="margin-top: 10px; margin-left: 5px;">
				<div style="float: left; margin-right: 5px;">
					<a href="#" class="coolButton buttonBGBlueGray" tabindex="30" onClick="window.parent.resourcesUpdateObj.saveFile(); return false;"><span class="save">Save</span></a>
				</div>
				<div style="float: left;">
					<a href="#" class="coolButton buttonBGRed" tabindex="30" onClick="window.parent.resourcesSupportObj.closeAddEditForm(); return false;"><span class="cancel">Cancel</span></a>
				</div>
				<div style="clear: left;"></div>
			</div>				
	</form>
</div>	
</body>	
</html>